package ee.ut.math.chess.base.model.chesspiece;

import java.util.ArrayList;
import java.util.List;

import ee.ut.math.chess.base.model.Move;
import ee.ut.math.chess.game.model.Game;

public class Bishop extends ChessPiece{

	private static final long serialVersionUID = 1L;
	private long value = 3;

	public Bishop(Color color) {
		this.color = color;
	}

	@Override
	public void setInInitialPosition(boolean inInitialPosition) {
		//not important;
	}

	@Override
	public boolean isInInitialPosition() {
		return false;//not important
	}

	@Override
	public List<Move> getLegalMoves(int x, int y, Game game, boolean onlyKingThreatening) {
		List<Move> result = new ArrayList<Move>();
		for (int i = 1; x + i < 8 && y + i < 8; i++) { //diagonally right & down
			int tmp = addMove(x, y, x + i, y + i, game, result);
			if (tmp != 0) {
				break;
			}
		}
		for (int i = 1; x + i < 8 && y - i >= 0; i++) { //diagonally right & up
			int tmp = addMove(x, y, x + i, y - i, game, result);
			if (tmp != 0) {
				break;
			}
		}
		for (int i = 1; x - i >= 0 && y + i < 8; i++) { //diagonally left & down
			int tmp = addMove(x, y, x - i, y + i, game, result);
			if (tmp != 0) {
				break;
			}
		}
		for (int i = 1; x - i >= 0 && y - i >= 0; i++) { //diagonally left & up
			int tmp = addMove(x, y, x - i, y - i, game, result);
			if (tmp != 0) {
				break;
			}
		}
		return result;
	}
	
	private int addMove(int x1, int y1, int x2, int y2, Game game,
			List<Move> result) {
		ChessPiece target = game.getBoard().getChessPiece(x2, y2);

		if (target == null) {
			result.add(new Move(x1, y1, x2, y2));
			return 0;// allowed
		}
		if (!this.color.equals(target.getColor())) {
			result.add(new Move(x1, y1, x2, y2));
			return 1;// last allowed
		}
		return 2; // not allowed
	}
	public long getValue() {
		return value;
	}
}
